﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class Login_DAO
    {
        SqlConnection cn = DataProvider.ConnectDB();
        

        //public DataSet Dangnhap(Nhanvien_DTO nv)
        //{
        //    DataSet ds = new DataSet();
            
        //    string sql = "SELECT * FROM NHANVIEN WHERE UserName='"+ nv.User+"' AND MatKhau='"+nv.Pass+"'";
        //    SqlDataAdapter da = new SqlDataAdapter(sql, cn);
        //    da.Fill(ds);
        //    cn.Close();
        //    return ds;          
        //}
        //public DataSet Dangnhap(Nhanvien_DTO nv)
        //{
        //    DataSet ds = new DataSet();
        //    SqlCommand cmd = new SqlCommand("sp_DangNhap",cn);
        //    cmd.CommandType = CommandType.StoredProcedure;
        //    cmd.Parameters.Add(new SqlParameter("@user", nv.User));
        //    cmd.Parameters.Add(new SqlParameter("@pass", nv.Pass));
        //    SqlParameter output = cmd.Parameters.Add(new SqlParameter("@msg", SqlDbType.NVarChar, 50));
        //    output.Direction = ParameterDirection.Output;
        //    SqlDataAdapter da = new SqlDataAdapter(cmd);
        //    da.Fill(ds);
        //    nv.Msg = output.Value.ToString();

        //    cn.Close();
        //    return ds;
        //}

        public Nhanvien_DTO Dangnhap(string user, string pass)
        {

            
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand("sp_DangNhap",cn);

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@user", user));
            cmd.Parameters.Add(new SqlParameter("@pass", pass));
            SqlParameter output = cmd.Parameters.Add(new SqlParameter("@msg", SqlDbType.NVarChar, 50));
            output.Direction = ParameterDirection.Output;

            SqlDataReader reader = cmd.ExecuteReader();

            Nhanvien_DTO NvDTO = new Nhanvien_DTO();
           
            while (reader.Read())
            {
                NvDTO.Ma_nv = reader.GetInt16(0);
                NvDTO.Hoten = reader.GetString(1);
                NvDTO.Sdt = reader.GetInt32(2);
                NvDTO.Ngaysinh = reader.GetDateTime(3);
                NvDTO.Diachi = reader.GetString(4);
                NvDTO.Gioitinh = reader.GetInt32(5);
                NvDTO.User = reader.GetString(6);
                NvDTO.Pass = reader.GetString(7);
                NvDTO.Vaitro = reader.GetString(8);
            }
            reader.Close();

            NvDTO.Msg = output.Value.ToString();
            
            cn.Close();
            return NvDTO;
        }

       
    }
}
